package com.ruoyi.project.dict.cizuaccess.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.cizuaccess.domain.CizuAccess;
import com.ruoyi.project.dict.cizuaccess.mapper.CizuAccessMapper;
import com.ruoyi.project.dict.cizuaccess.service.ICizuAccessService;
import com.ruoyi.project.dict.cizunew.domain.Cizu;
import com.ruoyi.project.dict.cizunew.mapper.CizuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 词组访问频率Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-07-07
 */
@Service
@DataSource(value = DataSourceType.DICT)
public class CizuAccessServiceImpl implements ICizuAccessService {
    @Autowired
    private CizuAccessMapper cizuAccessMapper;

    @Autowired
    private CizuMapper cizuMapper;

    /**
     * 查询词组访问频率
     *
     * @param ci 词组访问频率ID
     * @return 词组访问频率
     */
    @Override
    public CizuAccess selectCizuAccessById(String ci) {
        return cizuAccessMapper.selectCizuAccessById(ci);
    }

    /**
     * 查询词组访问频率列表
     *
     * @param cizuAccess 词组访问频率
     * @return 词组访问频率
     */
    @Override
    public List<CizuAccess> selectCizuAccessList(CizuAccess cizuAccess) {
        List<CizuAccess> list = cizuAccessMapper.selectCizuAccessList(cizuAccess);
        list.forEach(item -> {
            Cizu query = new Cizu();
            query.setCi(item.getCi());
            List<Cizu> cizus = cizuMapper.selectCizuList(query);
            if (!cizus.isEmpty()) {
                item.setStatus(1);
                Cizu cizu = cizus.get(0);
                if(!StringUtils.isEmpty(cizu.getYzjsAi())){
                    item.setKkcStatus(1);
                    item.setCiId(cizu.getId());
                }
            }
        });
        return list;
    }

    /**
     * 新增词组访问频率
     *
     * @param cizuAccess 词组访问频率
     * @return 结果
     */
    @Override
    public int insertCizuAccess(CizuAccess cizuAccess) {
        return cizuAccessMapper.insertCizuAccess(cizuAccess);
    }

    /**
     * 修改词组访问频率
     *
     * @param cizuAccess 词组访问频率
     * @return 结果
     */
    @Override
    public int updateCizuAccess(CizuAccess cizuAccess) {
        return cizuAccessMapper.updateCizuAccess(cizuAccess);
    }


    @Override
    public int importToCizu(Long id, String ci) {
        Cizu cizu = new Cizu();
        cizu.setCi(ci);
        return cizuMapper.insertCizu(cizu);
    }

    /**
     * 删除词组访问频率对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCizuAccessByIds(String ids) {
        return cizuAccessMapper.deleteCizuAccessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除词组访问频率信息
     *
     * @param ci 词组访问频率ID
     * @return 结果
     */
    @Override
    public int deleteCizuAccessById(String ci) {
        return cizuAccessMapper.deleteCizuAccessById(ci);
    }
}
